﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using Core;
using Data;
using Model;
using Web.Areas.sys.Controllers;

namespace Web.Areas.master.Controllers
{
    public class AgencyController : BaseController
    {
        //
        // GET: /master/Agent/
        public ActionResult Index() {
            var model = new {
                idField = "Agency_Id",
                urls = new {
                    query = "/api/master/agency",
                    add = "/master/agency/edit",
                    edit = "/master/agency/edit/",
                    remove = "/api/master/agency/edit/"
                },
                resx = new {
                    editTitle = "Edit Agency",
                    addTitle = "Add Agency",
                    removeComfirm = "Are you sure you want to delete this record?",
                    removeSuccess = "delete successfully!",
                    removeFailed = "delete failed!"
                },
                dataSource = new {
                    items = new Agency_MService().GetModels()
                },
                form = new {
                    Agency_Name = "",
                    Agency_desc = ""
                }
            };

            return View(model);
        }

        public ActionResult Edit(string id) {
            var data = new AgencyApiController().GetMaster(id);

            var model = new {
                form = data.form,
                urls = new {
                    edit = "/api/master/agency/edit"
                },
                resx = new {
                    saveSuccess = "Save Successfully!",
                    editSuccess = "Edit Successfully!"
                },
                dataSource = new { test = "test" },
                defaultForm = new Agency_M() {
                    Agency_Name = "",
                    Agency_Desc = ""
                }
            };

            return View(model);
        }
    }

    public class AgencyApiController : ApiController
    {
        private string _xml = "AgencyM.xml";
        Agency_MService _service = new Agency_MService();

        public dynamic Get(ReqWrapperQuery req) {
            var setting = req.ToParam(_xml);
            var ret = _service.GetDynamicListWithPaging(setting);
            return ret;
        }

        public dynamic GetMaster(string id) {
            var model = _service.GetModel(ParamQuery.Instance().AddWhere("Agency_Id", id));
            return new {
                form = model
            };
        }

        [System.Web.Http.HttpPost]
        public int Edit(ReqWrapperInsert req) {
            var pi = req.ToParam(_xml);
            return _service.Insert(pi);
        }

        [System.Web.Http.HttpPut]
        public int Edit(ReqWrapperUpdate req) {

            var pi = req.ToParam(_xml);
            return _service.Update(pi);
        }

        [System.Web.Http.HttpDelete]
        public int Edit(ReqWrapperDelete req) {
            return _service.Delete(req.ToParam(_xml));
            //return service.Update(new ParamBase().AddFieldUpdate("state", "2").AddWhere("Agency_Id", id));
        }

    }
}
